package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.uha.reggie.common.BaseContext;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.ShoppingCart;
import com.uha.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author uha
 * @date 2023/6/16
 * @apiNote
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     *
     */
    @PostMapping("/add")
    public ReggieResult<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //设置用户id  指定是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();

        // 获取不到currentId  ???

        //shoppingCart.setUserId(currentId);
        //log.info("++++++++++++++++++++++++++++++++++++++++");
        //log.info("将获取到的用户currentId放入: " + currentId);
        //log.info("+++++++++++++++++++++++++++++++++++++++++");
        shoppingCart.setUserId(2L);

        //先查询当前菜品或套餐在购物车

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        //queryWrapper.eq(ShoppingCart::getUserId,currentId);
        queryWrapper.eq(ShoppingCart::getUserId,2L);

        if (dishId != null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null){
            //存在 增加数量
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //不存在  加入购物车  数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;

        }

        return ReggieResult.success(cartServiceOne);
    }

    /**
     * 购物车减数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public ReggieResult<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){

        //设置用户id  指定是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();

        // 获取不到currentId  ???

        //shoppingCart.setUserId(currentId);
        shoppingCart.setUserId(2L);

        //先查询当前菜品或套餐在购物车

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        //queryWrapper.eq(ShoppingCart::getUserId,currentId);
        queryWrapper.eq(ShoppingCart::getUserId,2L);

        if (dishId != null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne.getNumber() != 1){
            //数量大于1  数量减1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //数量为1 删除此记录

            shoppingCartService.remove(queryWrapper);

            cartServiceOne = shoppingCart;

        }

        return ReggieResult.success(cartServiceOne);
    }


    /**
     * 清楚购物车
     * @return
     */
    @DeleteMapping("/clean")
    public ReggieResult<String> clean(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(ShoppingCart::getUserId,2L);

        shoppingCartService.remove(queryWrapper);

        return  ReggieResult.success("已清除购物车~");
    }






    /**
     * 购物车信息
     *
     */

    @GetMapping("/list")
    public ReggieResult<List<ShoppingCart>> list(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();

        //queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(ShoppingCart::getUserId,2L);

        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);


        return ReggieResult.success(list);
    }




}
